#pragma once

#include<iostream>
#include<functional>
#include<string>

#include"Log.hpp"

class Reactor;
namespace ConnectionSpace
{
    class Connection;
    using func= std::function<void(Connection)>;

    class Connection
    {
    public:
        Connection()
        {}
        
        Connection(int fd, uint32_t events,Reactor* reactor,func w=nullptr,func r=nullptr,func e=nullptr)
        :_fd(fd),
        _write(w),
        _read(r),
        _except(e),
        _reactor(reactor),
        _events(events)
        {}

        ~Connection()
        {}

        void AppendInBuf(const std::string& data)
        {
            _inbuf+=data;
        }

        void AppendOutBuf(const std::string& data)
        {
            _outbuf+=data;
        }

        string& GetInbuf()
        {
            return _inbuf;
        }

        string& GetOutbuf()
        {
            return _outbuf;
        }

        int GetFd()
        {
            return _fd;
        }

        uint32_t GetEvents()
        {
            return _events;
        }
        
        bool IsOutBufEmpty()
        {
            return _outbuf.empty();
        }

        void RegistWrite(func w)
        {
            _write=w;
        }

        void RegistRead(func r)
        {
            _read=r;
        }

        void RegistExcept(func e)
        {
            _except=e;
        }

    public:
        func _write;//写事件就绪执行函数
        func _read;//读事件就绪执行函数
        func _except;//事件出错执行函数
        Reactor* _reactor;//指向Reactor的指针，用于函数回调
    private:
        int _fd;//当前连接对应的文件描述符
        std:: string _inbuf;//读取到的数据
        std:: string _outbuf;//要发送的数据
        uint32_t _events;//需要监视的事件
    };
}